<%--
    Document   : index
    Created on : 19 juin 2010, 23:51:05
    Author     : marya
--%>

<%@page contentType="text/xml" pageEncoding="UTF-8"%>
<%@page import="org.restlet.resource.ClientResource" %>
<%@page import="org.restlet.representation.Representation" %>
<%@page import="org.restlet.ext.xml.DomRepresentation" %>
<%@page import="org.restlet.Response" %>
<%@page import="org.restlet.Request" %>
<%@page import="org.restlet.data.Method" %>
<%@page import="org.restlet.data.*" %>
<%@page import="org.w3c.dom.Element" %>
<%@page import="org.w3c.dom.Node" %>
<%@page import="org.w3c.dom.NodeList" %>
<%@page import="java.util.List" %>
<%@page import="java.util.Map" %>
<%@page import="xmlws.helpers.WebService" %>
<%
            /**
            Toujours préciser l'encodage
             */
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");

            ClientResource client = new ClientResource(WebService.federateur + "/contacts/" + request.getParameter("idContact"));
            /**
            Comportement différent selon la méthode HTTP de la requête
             */
            if (request.getMethod().equals("GET")) {
                DomRepresentation representation = new DomRepresentation(client.get());
                Node element = representation.getDocument().getDocumentElement();

                NodeList childs = element.getChildNodes();
                out.println("<form name=\"form_modifyContact\" action=\"#\" accept-charset=\"UTF-8\">");
                out.println("<table>");
                for (int j = 0; j < childs.getLength(); j++) {
                    Node child = childs.item(j);
                    String childName = child.getNodeName();
                    if (childName.equals("nom")) {
                        out.println("<tr><td>Nom : </td><td><input type=\"text\" name=\"nom\" value=\"" + child.getTextContent() + "\" /></td></tr>");
                    } else if (childName.equals("prenom")) {
                        out.println("<tr><td>Prénom : </td><td><input type=\"text\" name=\"prenom\" value=\"" + child.getTextContent() + "\" /></td></tr>");



                    } else if (childName.equals("emails")) {
                        NodeList emails = child.getChildNodes();
                        boolean hasEmailHome = false;
                        boolean hasEmailWork = false;
                        boolean hasEmailPrivate = false;
                        for (int k = 0; k < emails.getLength(); k++) {
                            Node email = emails.item(k);

                            if (email.getAttributes().getNamedItem("type").getTextContent().equals("Home")) {
                                out.println("<tr><td>Home Email : </td><td><input type=\"text\" name=\"emailHome\" value=\"" + email.getTextContent() + "\" /></td></tr>");
                                hasEmailHome = true;
                            }
                            if (email.getAttributes().getNamedItem("type").getTextContent().equals("Private")) {
                                out.println("<tr><td>Private Email : </td><td><input type=\"text\" name=\"emailPrivate\" value=\"" + email.getTextContent() + "\" /></td></tr>");
                                hasEmailPrivate = true;
                            }
                            if (email.getAttributes().getNamedItem("type").getTextContent().equals("Work")) {
                                out.println("<tr><td>Work Email : </td><td><input type=\"text\" name=\"emailWork\" value=\"" + email.getTextContent() + "\" /></td></tr>");
                                hasEmailWork = true;
                            }

                        }
                        if (!hasEmailHome) {
                            out.println("<tr><td>Home Email : </td><td><input type=\"text\" name=\"emailHome\" value=\"\" /></td></tr>");
                        }
                        if (!hasEmailPrivate) {
                            out.println("<tr><td>Private Email : </td><td><input type=\"text\" name=\"emailPrivate\" value=\"\" /></td></tr>");
                        }
                        if (!hasEmailWork) {
                            out.println("<tr><td>Work Email : </td><td><input type=\"text\" name=\"emailWork\" value=\"\" /></td></tr>");
                        }


                    } else if (childName.equals("tels")) {
                        NodeList tels = child.getChildNodes();
                        boolean hasTelHome = false;
                        boolean hasTelWork = false;
                        boolean hasTelPrivate = false;
                        for (int k = 0; k < tels.getLength(); k++) {
                            Node tel = tels.item(k);
                            if (tel.getAttributes().getNamedItem("type").getTextContent().equals("Home")) {
                                out.println("<tr><td>Home Tel : </td><td><input type=\"text\" name=\"telHome\" value=\"" + tel.getTextContent() + "\" /></td></tr>");
                                hasTelHome = true;
                            } else if (tel.getAttributes().getNamedItem("type").getTextContent().equals("Private")) {
                                out.println("<tr><td>Private Tel : </td><td><input type=\"text\" name=\"telPrivate\" value=\"" + tel.getTextContent() + "\" /></td></tr>");
                                hasTelPrivate = true;
                            } else if (tel.getAttributes().getNamedItem("type").getTextContent().equals("Work")) {
                                out.println("<tr><td>Work Tel : </td><td><input type=\"text\" name=\"telWork\" value=\"" + tel.getTextContent() + "\" /></td></tr>");
                                hasTelWork = true;
                            }
                        }
                        if (!hasTelHome) {
                            out.println("<tr><td>Home Tel : </td><td><input type=\"text\" name=\"telHome\" value=\"\" /></td></tr>");
                        }
                        if (!hasTelPrivate) {
                            out.println("<tr><td>Private Tel : </td><td><input type=\"text\" name=\"telPrivate\" value=\"\" /></td></tr>");
                        }
                        if (!hasTelWork) {
                            out.println("<tr><td>Work Tel : </td><td><input type=\"text\" name=\"telWork\" value=\"\" /></td></tr>");
                        }

                    } else if (childName.equals("adresse")) {
                        NodeList adresse = child.getChildNodes();
                        for (int k = 0; k < adresse.getLength(); k++) {
                            Node childAdresse = adresse.item(k);
                            String childAdresseName = childAdresse.getNodeName();
                            if (childAdresseName.equals("voie")) {
                                out.println("<tr><td>Voie : </td><td><input type=\"text\" name=\"voie\" value=\"" + childAdresse.getTextContent() + "\" /></td></tr>");
                            } else if (childAdresseName.equals("ville")) {
                                out.println("<tr><td>Ville : </td><td><input type=\"text\" name=\"ville\" value=\"" + childAdresse.getTextContent() + "\" /></td></tr>");
                            } else if (childAdresseName.equals("codePostal")) {
                                out.println("<tr><td>Code Postal : </td><td><input type=\"text\" name=\"codePostal\" value=\"" + childAdresse.getTextContent() + "\" /></td></tr>");
                            } else if (childAdresseName.equals("pays")) {
                                out.println("<tr><td>Pays : </td><td><input type=\"text\" name=\"pays\" value=\"" + childAdresse.getTextContent() + "\" /></td></tr>");
                            }
                        }
                    }
                }
                out.println("<tr><td>&nbsp;</td><td><input type=\"button\" class=\"button\" value=\"Enregistrer\" onclick=\"modify_contact(" + request.getParameter("idContact") + ")\"/></td></tr>");
                out.println("</table>");
                out.println("</form>");









            } else if (request.getMethod().equals("PUT")) {

                Form form = new Form();

                System.out.println(request.getQueryString());

                form.add("nom", request.getParameter("nom"));
                form.add("prenom", request.getParameter("prenom"));
                form.add("telHome", request.getParameter("telHome"));
                form.add("telPrivate", request.getParameter("telPrivate"));
                form.add("telWork", request.getParameter("telWork"));
                form.add("emailHome", request.getParameter("emailHome"));
                form.add("emailPrivate", request.getParameter("emailPrivate"));
                form.add("emailWork", request.getParameter("emailWork"));
                form.add("voie", request.getParameter("voie"));
                form.add("ville", request.getParameter("ville"));
                form.add("codePostal", request.getParameter("codePostal"));
                form.add("pays", request.getParameter("pays"));



                client.put(form.getWebRepresentation());

                if (client.getResponse().getStatus().equals(Status.SUCCESS_OK)) {
                    response.setStatus(200);


                    // Si le status est OK, on affiche le nouveau contact en faisant un get



                    client = new ClientResource(WebService.federateur + "/contacts/" + request.getParameter("idContact"));


                    DomRepresentation representation = new DomRepresentation(client.get());
                    Node element = representation.getDocument().getDocumentElement();
                    out.println("<table>");
                    NodeList childs = element.getChildNodes();
                    for (int j = 0; j < childs.getLength(); j++) {
                        Node child = childs.item(j);
                        String childName = child.getNodeName();
                        if (childName.equals("nom")) {
                            out.println("<tr><td>Nom : </td><td>" + child.getTextContent() + "</td></tr>");
                        } else if (childName.equals("prenom")) {
                            out.println("<tr><td>Prenom : </td><td>" + child.getTextContent() + "</td></tr>");

                        } else if (childName.equals("emails")) {
                            NodeList emails = child.getChildNodes();
                            for (int k = 0; k < emails.getLength(); k++) {
                                Node email = emails.item(k);
                                if (email.getAttributes().getNamedItem("type").getTextContent().equals("Home")) {
                                    out.println("<tr><td>Home Email : </td><td>" + email.getTextContent() + "</td></tr>");
                                }
                                if (email.getAttributes().getNamedItem("type").getTextContent().equals("Private")) {
                                    out.println("<tr><td>Private Email : </td><td>" + email.getTextContent() + "</td></tr>");
                                }
                                if (email.getAttributes().getNamedItem("type").getTextContent().equals("Work")) {
                                    out.println("<tr><td>Work Email : </td><td>" + email.getTextContent() + "</td></tr>");
                                }
                            }


                        } else if (childName.equals("tels")) {
                            NodeList tels = child.getChildNodes();
                            for (int k = 0; k < tels.getLength(); k++) {
                                Node tel = tels.item(k);
                                if (tel.getAttributes().getNamedItem("type").getTextContent().equals("Home")) {
                                    out.println("<tr><td>Home Tel : </td><td>" + tel.getTextContent() + "</td></tr>");
                                } else if (tel.getAttributes().getNamedItem("type").getTextContent().equals("Private")) {
                                    out.println("<tr><td>Private Tel : </td><td>" + tel.getTextContent() + "</td></tr>");
                                } else if (tel.getAttributes().getNamedItem("type").getTextContent().equals("Work")) {
                                    out.println("<tr><td>Work Tel : </td><td>" + tel.getTextContent() + "</td></tr>");
                                }

                            }



                        } else if (childName.equals("adresse")) {
                            NodeList adresse = child.getChildNodes();
                            for (int k = 0; k < adresse.getLength(); k++) {
                                Node childAdresse = adresse.item(k);
                                String childAdresseName = childAdresse.getNodeName();
                                if (childAdresseName.equals("voie")) {
                                    out.println("<tr><td>Voie : </td><td>" + childAdresse.getTextContent() + "</td></tr>");
                                } else if (childAdresseName.equals("ville")) {
                                    out.println("<tr><td>Ville : </td><td>" + childAdresse.getTextContent() + "</td></tr>");
                                } else if (childAdresseName.equals("codePostal")) {
                                    out.println("<tr><td>Code Postal : </td><td>" + childAdresse.getTextContent() + "</td></tr>");
                                } else if (childAdresseName.equals("pays")) {
                                    out.println("<tr><td>Pays : </td><td>" + childAdresse.getTextContent() + "</td></tr>");
                                }

                            }

                        }

                    }
                    out.println("<tr>");
                    out.println("<td colspan=\"2\">");
                    out.println("<a class='button' onclick=\"applyToModify_contact(" + request.getParameter("idContact") + ")\">Modifier</a>");

                    out.println("</td>");
                    out.println("</tr>");
                    out.println("</table><br/>");





                } else {
                    response.setStatus(client.getResponse().getStatus().getCode());
                    out.println(client.getResponseEntity().getText());
                }


            } else if (request.getMethod().equals("POST")) {
                //client = new ClientResource();

                Form form = new Form();

                client = new ClientResource(WebService.federateur + "/groupes/" + request.getParameter("idGroupe") + "/contacts");

                form.add("nom", request.getParameter("nom"));
                form.add("prenom", request.getParameter("prenom"));
                form.add("telHome", request.getParameter("telHome"));
                form.add("telPrivate", request.getParameter("telPrivate"));
                form.add("telWork", request.getParameter("telWork"));
                form.add("emailHome", request.getParameter("emailHome"));
                form.add("emailPrivate", request.getParameter("emailPrivate"));
                form.add("emailWork", request.getParameter("emailWork"));
                form.add("voie", request.getParameter("voie"));
                form.add("ville", request.getParameter("ville"));
                form.add("codePostal", request.getParameter("codePostal"));
                form.add("pays", request.getParameter("pays"));



                DomRepresentation representation = new DomRepresentation(client.post(form.getWebRepresentation()));

                if (client.getResponse().getStatus().equals(Status.SUCCESS_CREATED)) {
                    response.setStatus(200);
                    //Si le status est OK, on affiche le nouveau contact en faisant un get
                    //Sur l'id de contact contenu dans la representation de reponse

                    Node element = representation.getDocument().getDocumentElement();
                    String[] tab = element.getAttributes().
                            getNamedItem("href").getTextContent().split("/");

                    String id = tab[tab.length - 1];


                    client = new ClientResource(WebService.federateur + "/contacts/" + id);


                    representation = new DomRepresentation(client.get());
                    element = representation.getDocument().getDocumentElement();

                    out.println("<table>");
                    NodeList childs = element.getChildNodes();
                    for (int j = 0; j < childs.getLength(); j++) {
                        Node child = childs.item(j);
                        String childName = child.getNodeName();
                        if (childName.equals("nom")) {
                            out.println("<tr><td>Nom : </td><td>" + child.getTextContent() + "</td></tr>");
                        } else if (childName.equals("prenom")) {
                            out.println("<tr><td>Prenom : </td><td>" + child.getTextContent() + "</td></tr>");

                        } else if (childName.equals("emails")) {
                            NodeList emails = child.getChildNodes();
                            for (int k = 0; k < emails.getLength(); k++) {
                                Node email = emails.item(k);
                                if (email.getAttributes().getNamedItem("type").getTextContent().equals("Home")) {
                                    out.println("<tr><td>Home Email : </td><td>" + email.getTextContent() + "</td></tr>");
                                }
                                if (email.getAttributes().getNamedItem("type").getTextContent().equals("Private")) {
                                    out.println("<tr><td>Private Email : </td><td>" + email.getTextContent() + "</td></tr>");
                                }
                                if (email.getAttributes().getNamedItem("type").getTextContent().equals("Work")) {
                                    out.println("<tr><td>Work Email : </td><td>" + email.getTextContent() + "</td></tr>");
                                }
                            }


                        } else if (childName.equals("tels")) {
                            NodeList tels = child.getChildNodes();
                            for (int k = 0; k < tels.getLength(); k++) {
                                Node tel = tels.item(k);
                                if (tel.getAttributes().getNamedItem("type").getTextContent().equals("Home")) {
                                    out.println("<tr><td>Home Tel : </td><td>" + tel.getTextContent() + "</td></tr>");
                                } else if (tel.getAttributes().getNamedItem("type").getTextContent().equals("Private")) {
                                    out.println("<tr><td>Private Tel : </td><td>" + tel.getTextContent() + "</td></tr>");
                                } else if (tel.getAttributes().getNamedItem("type").getTextContent().equals("Work")) {
                                    out.println("<tr><td>Work Tel : </td><td>" + tel.getTextContent() + "</td></tr>");
                                }

                            }



                        } else if (childName.equals("adresse")) {
                            NodeList adresse = child.getChildNodes();
                            for (int k = 0; k < adresse.getLength(); k++) {
                                Node childAdresse = adresse.item(k);
                                String childAdresseName = childAdresse.getNodeName();
                                if (childAdresseName.equals("voie")) {
                                    out.println("<tr><td>Voie : </td><td>" + childAdresse.getTextContent() + "</td></tr>");
                                } else if (childAdresseName.equals("ville")) {
                                    out.println("<tr><td>Ville : </td><td>" + childAdresse.getTextContent() + "</td></tr>");
                                } else if (childAdresseName.equals("codePostal")) {
                                    out.println("<tr><td>Code Postal : </td><td>" + childAdresse.getTextContent() + "</td></tr>");
                                } else if (childAdresseName.equals("pays")) {
                                    out.println("<tr><td>Pays : </td><td>" + childAdresse.getTextContent() + "</td></tr>");
                                }

                            }

                        }

                    }
                    out.println("<tr>");
                    out.println("<td colspan=\"2\">");
                    out.println("<a class='button' onclick=\"applyToModify_contact(" + id + ")\">Modifier</a>");

                    out.println("</td>");
                    out.println("</tr>");
                    out.println("</table><br/>");





                } else {
                    response.setStatus(client.getResponse().getStatus().getCode());
                    out.println(client.getResponseEntity().getText());
                }


            } else if (request.getMethod().equals("DELETE")) {

                client.delete();
                if (client.getResponse().getStatus().equals(Status.SUCCESS_NO_CONTENT)) {
                    out.println("<span class=\"result\">Contact supprimé</span>");
                } else {
                    out.println(client.getResponseEntity().getText());
                }

            }







%>

